package com.sunda.spmsweb.overseacontroller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.SafetyInventorySearchDTO;
import com.sunda.spmsoversea.entity.SafetyInventory;
import com.sunda.spmsoversea.service.ISafetyInventoryService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 安全库存表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/safetyInventory")
@Api(tags = "安全库存", description = "安全库存")
public class SafetyInventoryController {

    @Autowired
    ISafetyInventoryService iSafetyInventoryService;

    @RequestMapping("/getSafetyInventoryPage")
    @ApiOperation(value = "查询安全库存分页数据", notes = "查询安全库存分页数据\n" +
            "工厂代码必输，物料编号可选\n" +
            "示例参数如下：\n" +
            "{\n" +
            "\t\"werks\": \"GF02\",\n" +
            "\t\"materialNo\": \"210052745\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"size\": 20\n" +
            "}", httpMethod = "POST")
    public ResponseResult getSafetyInventoryPage(@RequestBody SafetyInventorySearchDTO safetyInventorySearchDTO){
        if (StringUtils.isEmpty(safetyInventorySearchDTO.getWerks())){
            return ResponseResult.error("请求工厂不能为空");
        }
        try {
            IPage iPage = iSafetyInventoryService.getSafetyInventoryPage(safetyInventorySearchDTO);
            return ResponseResult.success().add("safetyInventoryList", iPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/createSafetyInventory")
    @ApiOperation(value = "新建或更新安全库存", notes = "新建或更新安全库存\n" +
            "根据 werks，materialNo 判断有效数据是否存在，存在则更新，不存在则新增；\n" +
            "示例参数如下：\n" +
            "{\n" +
            "\t\"comments\": \"c\",\n" +
            "\t\"dataVersion\": 7667,\n" +
            "\t\"materialNo\": \"210052745\",\n" +
            "\t\"safetyInventoryYear\": 0.5,\n" +
            "\t\"emergencyReserveBasicUnit\": 5,\n" +
            "\t\"basicUnit\": \"PCS\",\n" +
            "\t\"uuid\": \"d4b94a39f14b3428e0530101007fbcd2\",\n" +
            "\t\"werks\": \"GF02\",\n" +
            "\t\"whsLocationCode\": \"2006\",\n" +
            "\t\"safetyQtyBasicUnit\": 10,\n" +
            "\t\"standardConsumptionLastYear\": 0.5,\n" +
            "\t\"remarks\": \"r\"\n" +
            "}", httpMethod = "POST")
    public ResponseResult createSafetyInventory(@RequestBody SafetyInventory safetyInventory){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iSafetyInventoryService.createSafetyInventory(safetyInventory, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/deleteSafetyInventory")
    @ApiOperation(value = "删除安全库存数据", notes = "删除安全库存数据\n" +
            "示例数据：uuid = d4b94a39f14b3428e0530101007fbcd2, dataVersion = 7668", httpMethod = "POST")
    public ResponseResult deleteSafetyInventory(@RequestParam String uuid, @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iSafetyInventoryService.deleteSafetyInventory(uuid, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getStandardConsumption")
    @ApiOperation(value = "获取工厂物料上年度标准消耗用量", notes = "获取工厂物料上年度标准消耗用量\n" +
            "耗用量目前仅参考 OVERSEA_WHS_OUT_DTL、OVERSEA_WHS_OUT 出库任务表；\n" +
            "示例数据：werks = GF02, materialNo = 210051422 或 210050469，当前年分数据 yearNumber = 2022", httpMethod = "POST")
    public ResponseResult getStandardConsumption(@RequestParam String werks,
                                                 @RequestParam String materialNo,
                                                 @RequestParam Integer yearNumber){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iSafetyInventoryService.getStandardConsumption(werks, materialNo, yearNumber);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getBelowSafetyInventory")
    @ApiOperation(value = "获取低于安全库存报表数据", notes = "获取低于安全库存报表数据\n" +
            "获取指定工厂下，有安全库存数量信息 且 对应物料库存数量 低于 安全库存数量的数据\n" +
            "示例数据：werks = TF02", httpMethod = "POST")
    public ResponseResult getBelowSafetyInventory(@RequestParam String werks){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iSafetyInventoryService.getBelowSafetyInventory(werks);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
}
